package com.briup.pts.service.impl;

import com.briup.pts.domain.po.AnalysisPo;
import com.briup.pts.mapper.AnalysisMapper;
import com.briup.pts.service.AnalysisService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author 25095
 * @description
 * @data 2024/11/29-11:40
 */
@Service
@RequiredArgsConstructor
public class AnalysisServiceImpl implements AnalysisService {
    private final AnalysisMapper mapper;

    @Override
    public Map<String, Integer> indexCount() {
        // 获取数据
        List<AnalysisPo> analysisPos = mapper.indexCount();
        /*
        analysisPo ==> name 和 value
        name 就是 Map 集合的key
        value 就是Map 集合的value值
         */
        HashMap<String, Integer> map = new HashMap<>();
        for (AnalysisPo po : analysisPos) {
            map.put(po.getName(), po.getValue());
        }
        return map;
    }

    @Override
    public Map<String, Integer> countDisease() {
        // 获取数据
        List<AnalysisPo> analysisPos = mapper.countDisease();

        HashMap<String, Integer> map = new HashMap<>();
        for (AnalysisPo po : analysisPos) {
            map.put(po.getName(), po.getValue());
        }
        return map;
    }

    @Override
    public Map<String, List<?>> count() {
        List<AnalysisPo> list = mapper.count();
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> values = new ArrayList<>();
        list.forEach(po ->{
            names.add(po.getName());
            values.add(po.getValue());
        });
        return Map.of("name",names,"value",values);
    }

    @Override
    public Map<String, List<?>> countSales() {
        List<AnalysisPo> list =mapper.countSales();
        List<String> names = List.of("1月",
                "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月",
                "11月", "12月");
        List<Integer> values = new ArrayList<>(Collections.nCopies(12, 0));
        if (!CollectionUtils.isEmpty(list)){
            for (int i = 0,j =0;i<12 && j< list.size();i++) {
                AnalysisPo po = list.get(i);
                if (i+1==Integer.parseInt(po.getName())){
                    values.set(i,po.getValue());
                    j++;
                }
            }
        }
        return Map.of("name",names,"value",values);
    }
}
